// 轮播图swiper
var myProject = new Swiper('.myPro', {

    pagination: {
        el: '.swiper-pagination',
        clickable: true,
    },
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    loop: true,
    effect: 'fade',

    autoplay: true
});
myProject.el.onmouseover = function () {
    myProject.autoplay.stop()
}
myProject.el.onmouseout = function () {
    myProject.autoplay.start()
}
// 分页器功能
for (let i = 0; i < myProject.pagination.bullets.length; i++) {
    myProject.pagination.bullets[i].onmouseover = function () {
        this.click();
    };
};


let t = null;
$(function () {
// 特集商品 页面渲染
    $.ajax({
        type: "GET",
        url: "http://localhost:8888/public/home.json",
        dataType: "json",
        success: function (data) {
           render( data )
           //懒加载
           $('.lazy').lazyload()
        },
    })
    
    let frag_campaigns_wrap = $('.frag-campaigns-wrap');
    function render(res) {
        frag_campaigns_wrap.append(res.premium_goods.map(item => 
            `<div class="frag-campaigns-item">
                <div class="frag-campaigns-item-inner">
                    <div class="frag-campaigns-item-image">
                        <img class='lazy' data-original="${item.img}" alt="">
                        <div class="frag-campaigns-item-image-name">${item.name}</div>
                    </div>
                    <div class="frag-campaigns-item-name">${item.name}</div>
                </div>
            </div>`
        ).join(""))
    };

    //客服谈出二级菜单 客服
    $('.global-fab-service').mouseover(function(){
        $('.global-fab-details').show();
    });
    $('.global-fab-service').mouseout(function(){
        $('.global-fab-details').hide();
    });
    
    //节流
    $(window).scroll(function(){
        if( typeof t === "number"){
            return false;
        }
        t = setTimeout( function(){

            let scrollTop = $("html,body").scrollTop();

            if( scrollTop >= 850){
                $('.global-fab-backtotop').show();
            }else{
                $('.global-fab-backtotop').hide();
            }
            t = null;
        } , 150)
    });

    //箭头返回顶部
    $('.global-fab-backtotop').on('click' , function(){
        $("html,body")
        .animate({ scrollTop:0})
    })
    window.addEventListener('resize' , function(){
        let w = document.documentElement.clientWidth;
        if( w <= 1660 ){
            $('.global-sidebar').hide();
        }else{
            $('.global-sidebar').show();
        }
    })
    

    //楼梯
    let top_list = [];
    let itemContent = $('.itemContent');
    let animatte = false;

    $.each( itemContent , function( index , ele ){
        // console.log(index , ele);
        top_list.push( $(ele).offset().top );
    })
    $('.stairs').click( function(){
        var index = $(this).index('.stairs')
        let scrollTop = $("html,body").scrollTop();
        
        switch( index ){
            case 0 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  28
                })
            break;
            case 1 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  100
                })
            break;
            case 2 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  850
                })
            break;
            case 3 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  1155
                })
            break;
            case 4 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  1950
                })
            break;
            case 5 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  2800
                })
            break;
            case 6 :
                $("html,body")
                .stop( true )
                .animate({
                    scrollTop :  3590
                })
            break;
        }
    })
    
    //跳转详情页
    console.log($("#goodslist"));
    $('#goodslist').click(function(){
        console.log(1);
        location.href = "http://localhost:8080/src/html/goodlist.html"
    })


})


